import java.util.ArrayList;
import java.util.List;

public class MyTree {
    class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode rootNode;


    public TreeNode creatTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        rootNode = A;
        return A;
    }
    public TreeNode creatTree1() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');


        A.left = B;
        rootNode = A;
        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }


    //中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }


    //后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }

        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    //把前序遍历的结果放入顺序表中
    public List<Character> preorderTraversal(TreeNode root) {
        List<Character> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }

        ret.add(root.val);
        List<Character> leftTree = preorderTraversal(root.left);
        ret.addAll(leftTree);
        List<Character> rightTree = preorderTraversal(root.right);
        ret.addAll(rightTree);
        return ret;
    }

    //求节点的个数
    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftSize = size(root.left);
        int rightSize = size(root.right);
        return 1 + leftSize + rightSize;
    }

    //求叶子节点的个数
    public int getLeafNode(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        int leftSize = getLeafNode(root.left);
        int rightSize = getLeafNode(root.right);
        return leftSize + rightSize;
    }

    //求第k层节点的个数
    public int getLeveNode(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        int leftSize = getLeveNode(root.left, k - 1);
        int rightSize = getLeveNode(root.right, k - 1);
        return leftSize + rightSize;
    }

    public int getHightNode(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int leftHigh = getHightNode(root.left);
        int rightHigh = getHightNode(root.right);
        return (leftHigh > rightHigh) ? (leftHigh + 1) : (rightHigh + 1);

    }

    public TreeNode find(TreeNode root, char val) {

        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode left = find(root.left, val);
        if (left != null) {
            return left;
        }
        TreeNode right = find(root.right, val);
        if (right != null) {
            return right;
        }
        return null;
    }

    public boolean xiangtong(TreeNode root1, TreeNode root2) {
        if ((root1 != null && root2== null)||(root1==null&&root2!=null)) {
            return false;}
        if (root1 == null && root2 == null) {
            return true;
        }
        if(root1.val!=root2.val){
            return false;
        }
        boolean ret = xiangtong(root1.left, root2.left);
        if (ret == false) {
            return false;
        }
        boolean acc = xiangtong(root1.right, root2.right);
        if (acc == false) {
            return false;
        }

        return true;

    }
    public boolean ziShu(TreeNode root,TreeNode subRoot){
        if(root==null||subRoot==null){
            return false;
        }
        if(xiangtong(root,subRoot)){
            return true;
        }

        if(ziShu(root.left,subRoot))
        {
            return true;

        }
        if(ziShu(root.right,subRoot))
        {
            return true;

        }
        return false;
    }

//翻转二叉树
    public TreeNode invertTree(TreeNode root) {
        //递归函数的终止条件，节点为空时返回
        if(root==null) {
            return null;
        }
        //下面三句是将当前节点的左右子树交换
        TreeNode tmp = root.right;
        root.right = root.left;
        root.left = tmp;
        //递归交换当前节点的 左子树
        invertTree(root.left);
        //递归交换当前节点的 右子树
        invertTree(root.right);
        //函数返回时就表示当前这个节点，以及它的左右子树
        //都已经交换完了
        return root;
    }

    //平衡二叉树
    public boolean isBalanced(TreeNode root) {
        if(root==null){
            return true;
        }
        int left=mathHeight(root.left);
        int right=mathHeight(root.right);
        if(Math.abs(left-right)>1){
            return false;
        }
        return true;

    }
    public int mathHeight(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftHeight=mathHeight(root.left)+1;
        int rightHeight=mathHeight(root.right)+1;
        return leftHeight>rightHeight?leftHeight:rightHeight;
    }
}
